/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Madason.MetaModel
{
	/// <summary>
	/// Handles dependencies between UML-Models
	/// </summary>
	public class Dependency : ModelElement
	{
		private string sourceId;
		private string targetId;
		private Model model;

		public Dependency(Model model)
		{
			this.model = model;
		}


		public string SourceId
		{
			get { return sourceId; }
			set { sourceId = value; }
		}

		public string TargetId
		{
			get { return targetId; }
			set { targetId = value; }
		}

		public ModelElement Source
		{
			get { return model.FindModelElementById(sourceId); }
			
		}
		
		public ModelElement Target
		{
			get { return model.FindModelElementById(targetId);  }
			
		}
	}

	#region Helper Types

	/// <summary>
	/// List of classes
	/// </summary>
	public class DependencyList : ArrayList
	{
		/// <summary>
		/// Filter dependencies by a steretype
		/// </summary>
		/// <param name="st"></param>
		/// <returns></returns>
		public DependencyList Filter(Stereotype st)
		{
			DependencyList list = new DependencyList();
			foreach (Dependency dep in this)
				if (dep.Stereotype == st) list.Add(dep);
			return list;
		}


		/// <summary>
		/// Find all Namespaces with a dependency
		/// </summary>
		/// <returns></returns>
		public NamespaceList FindNamespaces()
		{
			NamespaceList result = new NamespaceList();
			foreach (Dependency dep in this)
				if (dep.Target != null && dep.Target is Namespace)
					result.Add(dep.Target as Namespace);
			return result;
		}


		/// <summary>
		/// Find all Classes with a dependency
		/// </summary>
		/// <returns></returns>
		public ClassList FindClasses()
		{
			ClassList result = new ClassList();
			foreach (Dependency dep in this)
				if (dep.Target != null && dep.Target is Class)
					result.Add(dep.Target as Class);
			return result;
		}


		/// <summary>
		/// Access class via its index
		/// </summary>
		/// <param name="i"></param>
		/// <returns></returns>
		public Dependency this[int i]
		{
			get
			{
				return base[i] as Dependency;
			}
			set
			{
				base[i] = value;
			}
		}

	}

	#endregion
}
